package auth

import (
"encoding/json"
"ez/vend/core"
"ez/vend/maker/mysql"
"ez/vend/tool"
"fmt"
"io"
"net/http"
"net/url"
"os"
"testing"
"ez/vend/doc/register"
)
const (
Model = "modelName"
Group = "groupName"
)

func CreatePageRegConfig() (root *register.Page) {
root = register.Create("模块占位")
root.SetModel(Model).SetGroup(Group).SetKey(fmt.Sprintf("%s.%s", Model, Group)).IsDir().AddSons(

//register.Create("目录占位").Copy(root).IsDir().AddSons(
//register.Create("列表页").Copy(root).AddKey("__TABLE__.list").SetRoute(fmt.Sprintf("/%s/%s", Model, Group)+"/__TABLE__/list").IsPage().AddSons(
//register.Create("_新增").Copy(root).AddKey("__TABLE__.Add").IsFunc(),
//register.Create("_编辑").Copy(root).AddKey("__TABLE__.edit").IsFunc(),
//register.Create("_删除").Copy(root).AddKey("__TABLE__.del").IsFunc(),
//),
//),

)
return
}

func TestRegPagesV1(t *testing.T) {
// 页面权限注册接口
apiRoot := core.GetConfig("dev::AuthRegApiRoot").String()
var regUrlPage = fmt.Sprintf("%s/Page/_reg_v1", apiRoot)
pages := []*register.Page{
CreatePageRegConfig(),
}
for _, data := range pages {
content, e := json.Marshal(data)
resp, e := http.PostForm(regUrlPage, url.Values{"content": []string{string(content)}})
if ez.PrintError(e) {
return
}
if resp != nil {
data, e := io.ReadAll(resp.Body)
if ez.PrintError(e) {
return
}
_, e = os.Stdout.Write(data)
ez.PrintError(e)
println("\n")
}
}


}
func TestRegApis(t *testing.T) {
// 接口权限注册接口
apiRoot := core.GetConfig("dev::AuthRegApiRoot").String()
var regUrlApi = fmt.Sprintf("%s/Api/_reg", apiRoot)
//var regUrlApi = "http://127.0.0.1:9991/auth/admin/Api/_reg"

// 注册当前模块下的默认页面和接口
// --注册默认页面
root := "./app/{{table.path}}/conf/orm"
files, e := os.ReadDir(root)
ez.PrintError(e)
// 接口注册
for _, file := range files {
c := mysql.Creator{}
content := ez.ReadFile(fmt.Sprintf("%s/%s", root, file.Name()))
_ = json.Unmarshal(content, &c)
resp, e := http.PostForm(regUrlApi, url.Values{"content": []string{string(content)}})
if ez.PrintError(e) {
continue
}
if resp != nil {
data, e := io.ReadAll(resp.Body)
if ez.PrintError(e) {
continue
}
_, e = os.Stdout.Write(data)
ez.PrintError(e)
println("\n")
}
}

}

func TestRegPages(t *testing.T) {
// 页面权限注册接口
apiRoot := core.GetConfig("dev::AuthRegApiRoot").String()
var regUrlPage = fmt.Sprintf("%s/Page/_reg", apiRoot)

// 注册当前模块下的默认页面和接口
// --注册默认页面
root := "./app/{{table.path}}/conf/orm"
files, e := os.ReadDir(root)
ez.PrintError(e)
// 页面注册
for _, file := range files {
c := mysql.Creator{}
content := ez.ReadFile(fmt.Sprintf("%s/%s", root, file.Name()))
_ = json.Unmarshal(content, &c)
resp, e := http.PostForm(regUrlPage, url.Values{"content": []string{string(content)}})
if ez.PrintError(e) {
continue
}
if resp != nil {
data, e := io.ReadAll(resp.Body)
if ez.PrintError(e) {
continue
}
_, e = os.Stdout.Write(data)
ez.PrintError(e)
println("\n")
}
}

}